/**
 * SerialTool.java
 * 版权所有(C) 2014
 * 创建者:cuiran 2014-8-11 下午4:47:03
 */
package com.ghyf.mplay.net.serial;

import java.io.File;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android_serialport_api.SerialPort;

import com.ghyf.mplay.util.LogUtil;


/**
 * 串口操作类 
 * @author cuiran
 * @version 1.0.0
 */
public class SerialTool {
	private static final String TAG="SerialTool";
	
	private OutputStream mOutputStream=null;
	private InputStream mInputStream=null;
	private SerialPort sp=null;
	 ReadThread  mReadThread=null;
	
	 /**
	  * 打开串口<br>
	  * 2014-8-12 下午1:09:23
	  * @param path
	  *
	  */
	public void openSeria(String path){
		try {
			 LogUtil.i(TAG, "串口开始打开...");	
			 sp=new SerialPort(new File(path),38400,0);
			 if(sp==null)return;
			 mInputStream=sp.getInputStream();
			 LogUtil.i(TAG, "串口打开成功...");
//			 mReadThread=new ReadThread();
//			 mReadThread.start();
			 
		} catch (SecurityException e) {
			LogUtil.e(TAG,"SecurityException",e);
		} catch (IOException e) {
			LogUtil.e(TAG,"openSeria",e);
		}finally{
//			if(mInputStream!=null){
//				try {
//					mInputStream.close();
//					mInputStream=null;
//				} catch (IOException e) {
//					LogUtil.e(TAG,"openSeria",e);
//				}
//				
//			}
		}
	
	}

	
	/**
	 * 往串口写入数据<br>
	 * 2014-9-10 上午11:26:38
	 * @param data
	 *
	 */
	public void writeSeria(String data){

		try {
			 LogUtil.i(TAG, "串口开始写入...");	
			mOutputStream=sp.getOutputStream();
			mOutputStream.write(new String(data).getBytes());
			mOutputStream.write('\n');
			 LogUtil.i(TAG, "串口写入成功...");
		} catch (IOException e) {
			LogUtil.e(TAG,"writeSeria",e);
		}finally{
//			if(mOutputStream!=null){
//				try {
//					mOutputStream.close();
//					mOutputStream=null;
//				} catch (IOException e) {
//					LogUtil.e(TAG,"openSeria",e);
//				}
//				
//			}
		}
		  
	}
	
	public void writeSeria(byte [] data){

		try {
			 LogUtil.i(TAG, "串口开始写入...");	
			mOutputStream=sp.getOutputStream();
			mOutputStream.write(data);
//			mOutputStream.write('\n');
			 LogUtil.i(TAG, "串口写入成功...");
		} catch (IOException e) {
			LogUtil.e(TAG,"writeSeria",e);
		}finally{
//			if(mOutputStream!=null){
//				try {
//					mOutputStream.close();
//					mOutputStream=null;
//				} catch (IOException e) {
//					LogUtil.e(TAG,"openSeria",e);
//				}
//				
//			}
		}
		  
	}
	
	private class ReadThread extends Thread {

		@Override
		public void run() {
			super.run();
			 LogUtil.i(TAG, "ReadThread start ...");
			while(!mReadThread.isInterrupted()) {
				int size;
				try {
					byte[] buffer = new byte[4];
					if (mInputStream == null) return;
					size = mInputStream.read(buffer);
					LogUtil.i(TAG, "ReadThread start size="+size);
					if (size > 0) {
						onDataReceived(buffer, size);
					}
					Thread.sleep(3000);
				} catch (IOException e) {
					LogUtil.e(TAG,"writeSeria",e);
					return;
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	protected void onDataReceived(byte[] buffer, int size) {
		// ignore incoming data
		 LogUtil.i(TAG, "串口onDataReceived..."+buffer.length+" size="+size);
	}
}
